<template>
  <div class="dashboard-container">
    <!-- 搜索区域-->
    <el-col :xs="24">
      <el-input
        v-model="fileName"
        placeholder="请输入文件名"
        style="width: 20%"
        type="text"
      ></el-input>
      <el-button type="primary" @click="getDocumentList" icon="el-icon-search"
        >查询</el-button
      >
    </el-col>
    <!-- 搜索区域-->

    <!--数据表格区域-->
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" label="ID" width="100">
        <template slot-scope="scope">
          {{ scope.row.id }}
        </template>
      </el-table-column>

      <el-table-column align="center" label="标题" width="120">
        <template slot-scope="scope">
          {{ scope.row.title }}
        </template>
      </el-table-column>

      <el-table-column align="center" label="文件名" width="120">
        <template slot-scope="scope">
          {{ scope.row.fileName }}
        </template>
      </el-table-column>

      <el-table-column align="center" label="文件类型" width="120">
        <template slot-scope="scope">
          {{ scope.row.filetype }}
        </template>
      </el-table-column>

      <el-table-column align="center" label="描述" width="140">
        <template slot-scope="scope">
          {{ scope.row.remark }}
        </template>
      </el-table-column>

      <el-table-column align="center" label="上传时间" width="120">
        <template slot-scope="scope">
          {{ scope.row.createDate }}
        </template>
      </el-table-column>

      <el-table-column align="center" label="上传人" width="100">
        <template slot-scope="scope">
          {{ scope.row.user.username }}
        </template>
      </el-table-column>

      <el-table-column align="center" label="操作" width="120">
        <template slot-scope="scope">
              <!--添加绑定事件-->
              <el-button @click="deleteHandle(scope.row.id)" type="text" size="small">删除</el-button>
              </template>
      </el-table-column>

      <el-table-column align="center" label="下载" width="120">
        <template slot-scope="scope">
          <!--添加绑定事件-->
          <a @click="downloadHandle(scope.row.id)" type="text" size="small">下载</a
          >
        </template>
      </el-table-column>
    </el-table>
    <!--数据表格区域-->

    <!--分页区域-->
    <el-pagination
      background
      layout="prev,pager,next"
      :page-size="pageSize"
      :current-page="pageIndex"
      :total="totalRecoreSum"
      @current-change="checkPage"
    >
      <!--分页区域-->
    </el-pagination>
  </div>
</template>

<script>
//引入对象
import { axios } from "@/plugins/axios";
import { mapGetters } from "vuex";

export default {
  data() {
    return {
      list: null,
      listLoading: true,
      totalRecoreSum: 0,
      pageIndex: 1, //当前页码
      pageSize: 2, //每页显示数量
      UploadVisible: false,
      fileName: "",
      title: "",
      filetype: "",
      remark: "",
      createDate: "",
      user: "",
      id: "",
    };
  },
  created() {
    this.getDocumentList();
  },
  methods: {
    //页数跟页码，每页显示数量
    init() {
      this.list = [];
      this.totalRecoreSum = 0;
      this.pageIndex = 1;
      this.pageSize = 2;
    },

    //显示下一页
    checkPage(val) {
      //alert(this.pageIndex);
      this.pageIndex = val;
      //调用getUserList()方法
      this.getDocumentList();
    },

    deleteHandle(id){
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          //异步请求url
          const url='http://localhost:8080/hrm/deleteDocument?id='+id;
          //传进url
         this.$axios.post(url).then(function(response){
          //判断
           if(response.data){
             this.message({
                 message:'成功删除',
                  type:'success',
              })
            }
        })
        //刷新列表，调用方法
        this.getDocumentList()
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
        
    },

    //下载
    downloadHandle(id) {
      var $this = this;
      const url = `http://localhost:8080/hrm/downloadDocument`;
      this.$axios({
        methods:"post",
        url:url,
        responseType:"blob",
        params:{id},
      }).then((response)=>{
        console.log(response.data)
        if (response.data) {
          const url=URL.createObjectURL(new Blob([response.data]));
          const link=document.createElement("a");
          link.href=url;
          link.setAttribute("download",id);
          document.body.appendChild(link);
          link.click();
          document.body.removeChild(link);
          this.$message({
            message: "成功下载",
            type: "success",
          });
        }
      });
    },

    //页码
    getDocumentList() {
      const url =
        `http://localhost:8080/hrm/findAlldocument?pageIndex=` + this.pageIndex+`&fileName=`+this.fileName;
      var $this = this;
      this.$axios.post(url).then(function (response) {
        if (response.data) {
          $this.listLoading = false;
          $this.list = response.data.documents;
          $this.totalRecoreSum = response.data.model.totalRecoreSum;
        }
      });
    },
    handleClick() {},
  },
};
</script>

<style lang="scss" scoped>
.dashboard {
  &-container {
    margin: 30px;
  }
  &-text {
    font-size: 30px;
    line-height: 46px;
  }
}
</style>
